package com.googlecode.jlogindexer.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.response.SolrPingResponse;

import com.googlecode.jlogindexer.document.LogDocument;

public class SolrUtils {
	
	private String solrUrl;
	private CommonsHttpSolrServer solrServer=null;
	private boolean isSolrServerUp=false;
	private int docCount=0;
	
	private static long SERVER_POLL_DELAY=5000;
	private static int PING_TIMEOUT=500;
	
	
	private Timer timerService=null;
	
	
	private SolrUtils() {
		//TODO Initiate local server otherwise
	}
	
	public SolrUtils(String solrUrl){
		this.solrUrl=solrUrl;
	}
	

	public void initServer() {
		try {
		    
			solrServer =new CommonsHttpSolrServer(solrUrl);
			timerService=new Timer();
			resolveServerAvailability();
			startTickingTimer();
		} catch (MalformedURLException e) {
			System.err.println("Unable to resolve Solr Url"+e.getMessage());
		}
			
	}

	public boolean isServerAvailable() {
	
		return isSolrServerUp;
	}
	
	/**
	 * Resolves whether the Solr server is up and running. Sets the isSolrServerUp field accordingly. 
	 * TODO Could bring up the local server if false.
	 */
	private void resolveServerAvailability() {
		/*try {
			
			if (solrServer!=null){
				System.out.println("Pinging serverrrr now....");
//				SolrPingResponse solrResponse = solrServer.ping();
//				
//				System.out.println("Ping threw an exception....");
//				int status = solrResponse.getStatus();
				
				int status=pingSolrServer();
				//System.out.println("Server STATUS IS "+status);
					if (status==0){
						System.err.println("Server status is active....");
						isSolrServerUp=true;
					}
					else{
						System.err.println("Server status is shutdown.... ");
						isSolrServerUp=false;
					}
			}
			else{
				isSolrServerUp=false;
			}
			
		} catch (SolrServerException e) {
			isSolrServerUp=false;
			System.err.println("Could not connect to Solr Server. Possibly Down?"+e.getMessage());
		} 
		catch (IOException e) {
			isSolrServerUp=false;
			System.err.println("Could not connect to Solr Server. Possibly Down?"+e.getMessage());
		}*/
		
			
			try {
				if (solrServer!=null){
					System.out.println("Pinging serverrrr now....");
					SolrPingResponse solrResponse = solrServer.ping();
//				
					System.out.println("Ping tdone......");
					int status = solrResponse.getStatus();
					
					//int status=pingSolrServer();
					System.out.println("@@@@@@@@@@@@$%^&*()*&^%$#%^&*()*&^%$#%^&*()_(*&^%$#$%^&*()_(*&^%$#$%^&*()*&^%$#$%^&*()Server STATUS IS "+status);
						if (status==0){
							System.err.println("Server status is active....");
							isSolrServerUp=true;
						}
						else{
							System.err.println("Server status is shutdown.... ");
							isSolrServerUp=false;
						}
				}
				else{
					isSolrServerUp=false;
				}
			} catch (SolrServerException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		
				
	}

	private void startTickingTimer() {
		timerService.schedule(new SolrPoller(), SERVER_POLL_DELAY);
		
	}
	
	
	private class SolrPoller extends TimerTask{
		
		public void run() {
			System.err.println("Server getting Pollled right now........");
			System.gc();
			resolveServerAvailability();
		}
		
	};


	public void shutDownServer() {
		// TODO Need to identify what exactly shutting down client means in Solr world. As of now, I am just nullifying the solrserver. 
		//WHAT THE HELL happened to the http connection?
		System.err.println("Shutting down server???");
		try {
			solrServer.commit(); //Commit the unflushed records
		} catch (SolrServerException e) {
			System.err.println("Problem closing Solr server client connection"+e.getMessage());
		} catch (IOException e) {
			System.err.println("Problem closing Solr server client connection"+e.getMessage());
		}
		
	}

	public void addDocumentToSolr(LogDocument logDocument) {
		try {
			solrServer.addBean(logDocument);
			docCount++;
			if (docCount%10==0){
				solrServer.commit();
				System.gc();
			}
		} catch (SolrServerException e) {
			System.err.println("Could not add document to Solr Server. Possibly Down?"+e.getMessage());
		} 
		catch (IOException e) {
			System.err.println("Could not add document to Solr Server. Possibly Down?"+e.getMessage());
		}
	}
	
	
	
}